/**
 * ---------------------------
 * Time: 2017/8/6 18:28.
 * Author: Cangshi
 * View: http://palerock.cn
 * ---------------------------
 */
define(['app', 'zepto'], function (app, $) {
    /**
     * 事故注册控制器
     */
    app.controller('accident-register', function ($http, $scope, accidentService, SafetyAccident) {
        var init = function () {
            // 定义安全等级
            $scope.levelOption = [
                {
                    description: '一般安全事故',
                    level: SafetyAccident.NORMAL_LEVEL
                }, {
                    description: '重大安全事故',
                    level: SafetyAccident.MAJOR_LEVEL
                }, {
                    description: '特大安全事故',
                    level: SafetyAccident.SERIOUS_LEVEL
                }
            ];
            $scope.accident = {
                accidentLevel: SafetyAccident.NORMAL_LEVEL
            };
        };
        init();
        $scope.register = function () {
            accidentService.addAccident(new SafetyAccident($scope.accident), function (id) {
                $.alert('注册成功，事故id为:' + id, function () {
                    history.back();
                });
            });
        };
    });

    /**
     * 安全数据页面
     */
    app.controller('accident-main', function ($http, $scope, accidentService, ArrayUtils, SafetyAccident) {
        var init = function () {
            $scope.accidents = [];
            // 获取所有的事故
            accidentService.findAllAccidents(function (accidents, message) {
                $scope.accidents = accidents;
                // 统计数据
                $scope.statistics = {
                    // 事故总数
                    totalAccidentsNum: accidents.length,
                    totalMoneyCost: 0,
                    normalLevel: {
                        num: 0,
                        cost: 0
                    },
                    majorLevel: {
                        num: 0,
                        cost: 0
                    },
                    seriousLevel: {
                        num: 0,
                        cost: 0
                    }
                };
                // 遍历所有事故
                ArrayUtils.ergodicArrayObject(this, accidents, function (accident) {
                    $scope.statistics.totalMoneyCost += accident.accidentCostMoney;
                    switch (accident.accidentLevel) {
                        default:
                        case SafetyAccident.NORMAL_LEVEL:
                            $scope.statistics.normalLevel.num++;
                            $scope.statistics.normalLevel.cost += accident.accidentCostMoney;
                            break;
                        case SafetyAccident.MAJOR_LEVEL:
                            $scope.statistics.majorLevel.num++;
                            $scope.statistics.majorLevel.cost += accident.accidentCostMoney;
                            break;
                        case SafetyAccident.SERIOUS_LEVEL:
                            $scope.statistics.seriousLevel.num++;
                            $scope.statistics.seriousLevel.cost += accident.accidentCostMoney;
                            break;
                    }
                });

                // 执行脏检测
                $scope.$digest();
            }, {
                sortBy: 'accidentId',
                order: 0 // 降序
            });
        };
        init();

    });

    /**
     * 单个事故详情
     */
    app.controller('accident-detail', function ($http, $scope, $routeParams, accidentService) {
        var accidentId;
        var init = function () {
            if ($routeParams.accidentId == null || Number.isNaN(parseInt($routeParams.accidentId))) {
                app.backEvent();
                return;
            }
            accidentId = parseInt($routeParams.accidentId);
            $scope.accident = {};
            accidentService.findAccidentById(accidentId, function (accident, e) {
                $scope.accident = accident;
                $scope.$digest();
            }, function () {
                history.back();
            });
        };
        init();

        $scope.removeThis = function () {
            $.confirm('确认删除当前安全事件？', function () {
                accidentService.deleteAccident($scope.accident.accidentId, function () {
                    $.alert('删除该事件成功', function () {
                        history.back();
                    })
                });
            });
        }
    });
})
;